//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package project_code_api_serv

import (
	"context"
	"fmt"

	"atomgit.com/openlinksaas-org/api-server/api_common"
	"atomgit.com/openlinksaas-org/api-server/dao"
	"atomgit.com/openlinksaas-org/api-server/dao/code_dao"
	"atomgit.com/openlinksaas-org/api-server/dao/project_dao"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/events/events_code"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/events_api"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/project_code_api"
	"github.com/google/uuid"
	"go.mongodb.org/mongo-driver/mongo"
	"google.golang.org/protobuf/types/known/anypb"
)

func (apiImpl *ProjectCodeApiImpl) addAddCommentEvent(
	sessionItem *dao.SessionDbItem,
	req *project_code_api.AddCommentRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	commentId string, nowTime int64) {
	eventData, err := anypb.New(&events_code.AddCommentEvent{
		CommentId:   commentId,
		ThreadId:    req.ThreadId,
		ContentType: uint32(req.ContentType),
		Content:     req.Content,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_CODE,
			EventTime:       nowTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_CODE_COMMENT_THREAD,
			RefId:           req.ThreadId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectCodeApiImpl) addUpdateCommentEvent(
	sessionItem *dao.SessionDbItem,
	req *project_code_api.UpdateCommentRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	commentItem *code_dao.CommentDbItem, nowTime int64) {
	eventData, err := anypb.New(&events_code.UpdateCommentEvent{
		CommentId:      req.CommentId,
		ThreadId:       commentItem.ThreadId,
		OldContentType: commentItem.ContentType,
		NewContentType: uint32(req.ContentType),
		OldContent:     commentItem.Content,
		NewContent:     req.Content,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_CODE,
			EventTime:       nowTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_CODE_COMMENT_THREAD,
			RefId:           commentItem.ThreadId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}

func (apiImpl *ProjectCodeApiImpl) addRemoveommentEvent(
	sessionItem *dao.SessionDbItem,
	req *project_code_api.RemoveCommentRequest,
	projectItem *project_dao.ProjectInfoDbItem,
	commentItem *code_dao.CommentDbItem, nowTime int64) {
	eventData, err := anypb.New(&events_code.RemoveCommentEvent{
		CommentId:   req.CommentId,
		ThreadId:    commentItem.ThreadId,
		ContentType: commentItem.ContentType,
		Content:     commentItem.Content,
	})
	if err != nil {
		fmt.Println(err)
		return
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		fmt.Println(err)
		return
	}
	defer dao.EndSession(dbSess)
	dbSess.WithTransaction(context.Background(), func(sessCtx mongo.SessionContext) (interface{}, error) {
		err := api_common.AddEvent(sessCtx, &events_api.Event{
			EventId:         uuid.NewString(),
			UserId:          sessionItem.UserId,
			UserDisplayName: sessionItem.DisplayName,
			ProjectId:       req.ProjectId,
			ProjectName:     projectItem.BasicInfo.ProjectName,
			EventType:       events_api.EVENT_TYPE_EVENT_TYPE_CODE,
			EventTime:       nowTime,
			RefType:         events_api.EVENT_REF_TYPE_EVENT_REF_TYPE_CODE_COMMENT_THREAD,
			RefId:           commentItem.ThreadId,
			EventData:       eventData,
		}, false, "", "")
		if err != nil {
			fmt.Println(err)
		}
		return nil, err
	})
}
